/**
 * 
 */
package nepomuk.sandbox;

import java.util.Properties;

import javax.servlet.ServletException;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.service.http.HttpContext;
import org.osgi.service.http.HttpService;
import org.osgi.service.http.NamespaceException;
import org.semanticdesktop.common.osgi.MultiServiceTracker;
import org.semanticdesktop.common.osgi.OSGIObjectActivator;
import org.semanticdesktop.common.osgi.OSGIObjectDeactivator;
import org.semanticdesktop.common.osgi.OSGIServiceActivator;
import org.semanticdesktop.common.osgi.OSGIServiceDeactivator;

/**
 * @author kotelnikov
 */
public class Activator implements BundleActivator {

    private BundleContext fContext;

    private HttpService fHttpService;

    private TestServlet fServlet;

    private final String fServletPath = "/";

    private MultiServiceTracker fTracker;

    @OSGIObjectActivator
    public void activate() throws ServletException, NamespaceException {
        fServlet = new TestServlet(fContext);
        HttpContext context = fHttpService.createDefaultHttpContext();
        fHttpService.registerServlet(
            fServletPath,
            fServlet,
            new Properties(),
            context);
    }

    @OSGIObjectDeactivator
    public void deactivate() {
        fHttpService.unregister(fServletPath);
    }

    @OSGIServiceDeactivator
    public void removeHttpService(HttpService service) {
        fHttpService = null;
    }

    @OSGIServiceActivator
    public void setHttpService(HttpService service) {
        fHttpService = service;
    }

    public void start(BundleContext context) throws Exception {
        new RDFExample().run();
        fContext = context;
        fTracker = new MultiServiceTracker(context, this);
        fTracker.open();
    }

    public void stop(BundleContext context) throws Exception {
        fTracker.close();
    }

}
